﻿/////////////////////////////////////////////////////////////////////////
//                                                                     //
//    MTGProject - A Magic The Gathering Networked Environment         //
//                                                                     //
//  Copyright (c) 2008-2009 by The MTGProject Team (CSC230 & MAT227)   //
//                                                                     //
// This file is part of MTGProject.                                    //
//                                                                     //
// MTGProject is free software; you can redistribute it and/or         //
// modify it under the terms of the GNU Lesser General Public          //
// License as published by the Free Software Foundation; either        //
// version 3 of the License, or (at your option) any later version.    //
//                                                                     //
// This library is distributed in the hope that it will be useful,     //
// but WITHOUT ANY WARRANTY; without even the implied warranty of      //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       //
// GNU Lesser General Public License for more details.                 //
//                                                                     //
// You should have received a copy of the GNU Lesser General Public    //
// License along with this library; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA           //
// 02111-1307, USA, or contact the author(s):                          //
//                                                                     //
// Ruben Acuna <flyingfowlsoftware@earthlink.net>                      //
// Ryan Costello <fusioncobra@yahoo.com>                               //
//                                                                     //
/////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;

namespace MTGProject
{
    abstract public class PlayerController
    {
        #region abstract properties

        abstract public Player Puppet
        {
            get;
            set;
        }

        abstract public bool IsConnected
        {
            get;
            set;
        }

        #endregion

        #region methods

        public void PhaseCombat()
        {
            LinkedList<CardCreature> attackingCreatures = new LinkedList<CardCreature>();
            int floatingDamage = 0; //KEYWORD: TRAMPLE
            PhaseCombatStart();

            CardCreature attacker = PhaseCombatRequestAttackingCreature();
            while (attacker != null)
            {
                attackingCreatures.AddLast(attacker);
                attacker = PhaseCombatRequestAttackingCreature();
            }

            //KEYWORD: EXALTED
            if (attackingCreatures.Count == 1)
            {
                int exaltedCreatures = 0;

                //count number of attacker's exalted cards.
                foreach (IPermanent permanent in Puppet.Permanents.Cards)
                    if (permanent is CardCreature)
                        if (((CardCreature)permanent).HasKeyword(CreatureKeyword.EXALTED))
                            exaltedCreatures++;

                //add counters.
                attackingCreatures.First.Value.TemporaryCountersOSO += exaltedCreatures; 
            }

            foreach (CardCreature creature in attackingCreatures)
                creature.OnAttacks(); //SCRIPTING HOOK

            if(attackingCreatures.Count !=0)
                floatingDamage = ((PlayerController)Puppet.Enemy.PuppetMaster).PhaseDefense(attackingCreatures); //KEYWORD: TRAMPLE

            Puppet.Enemy.TakeDamage(floatingDamage, null); //KEYWORD: TRAMPLE

            //do damage to enemy with all remaining lands
            foreach (CardCreature creature in attackingCreatures)
                Puppet.AttackWithCard(creature);

            PhaseCombatEnd();
        }

        #endregion

        #region abstract methods

        abstract public void PhaseMain();

        abstract public void PhaseCombatStart();
        abstract public CardCreature PhaseCombatRequestAttackingCreature();
        abstract public void PhaseCombatEnd();

        abstract public int PhaseDefense(LinkedList<CardCreature> unblockedAttackers);

        abstract public void OnLose();
        abstract public void OnWin();

        abstract public ICard SelectCard(LinkedList<ICard> cards); //TODO: remove this.
        abstract public ICard SelectCard(LinkedList<ICard> cards, string message);

        abstract public void Tell(string message);

        #endregion
    }
}
